package sctu.edu.java.year2016.team02.project.pc.UI;

import sctu.edu.java.year2016.team02.project.pc.loginMan.Operator;
import sctu.edu.java.year2016.team02.project.pc.problem.PcProblem;
import sctu.edu.java.year2016.team02.project.pc.problem.SocketListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * Created by Eoly on 2016/6/12.
 */
//呈现具体问题的具体内容的对话框
public class WatchUI extends JDialog implements ActionListener{
    private JLabel MainText;
    private JLabel MainMessage;
    private JLabel DetailMessage;
    private JLabel SolveMessage;
    private JLabel isSolved;
    private JLabel message;
    private JTextArea DetailText;
    private JTextArea SolvedText;
    private JPanel insertPanel;
    private Operator op;
    private Button answerIt;
    private Button deleteIt;
    private JLabel root;
    private Object[] SResult;

    public WatchUI(JFrame parent, String title, Object[] list, boolean modal, Operator op) {
        super(parent, title, modal);
        this.op = op;
        SResult = list;
        Init();
        setLayout(null);
        add(insertPanel);
        add(answerIt);
        add(deleteIt);
        add(root);
        setBounds(SocketListener.screenWidth / 3, SocketListener.ScreenHeight / 4, 600, 510);
        setResizable(false);
        setVisible(true);
    }

    private void Init() {
        insertPanel = new JPanel();
        message = new JLabel("submit by: " + SResult[0]);//SResult[0] 用户信息
        MainMessage = new JLabel("问题简要：");
        MainText = new JLabel("  " + SResult[2]);//SResult[1] 问题简要
        DetailMessage = new JLabel("问题详细描述：");
        SolveMessage = new JLabel("解决方案：");
        isSolved = new JLabel("（是否解决：" + PcProblem.isSolved(SResult[5]) + "）");//SResult[5] 是否解决
        DetailText = new JTextArea((String)SResult[3]);//SResult[2] 问题详细描述
        SolvedText = new JTextArea((String)SResult[4]);//SResult[4] 问题解决方案
        answerIt = new Button("Answer it");
        deleteIt = new Button("Delete it");
        if(!op.isAD) {
            answerIt.setEnabled(false);
            deleteIt.setEnabled(false);
        }
        root = new JLabel("ROOT:"+(op.isAD?"ADMIN":"PC"));
        insertPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        insertPanel.setBounds(5, 5, 450, 470);
        message.setBounds(270, 5, 180, 25);
        message.setEnabled(false);
        MainMessage.setBounds(10, 10, 100, 25);
        MainText.setBounds(10, 40, 430, 25);
        MainText.setBorder(BorderFactory.createEtchedBorder());
        DetailMessage.setBounds(10, 70, 100, 25);
        SolveMessage.setBounds(10, 270, 100, 25);
        isSolved.setBounds(100, 270, 150, 25);
        DetailText.setEditable(false);
        DetailText.setLineWrap(true);
        SolvedText.setEditable(false);
        SolvedText.setLineWrap(true);
        insertPanel.setLayout(null);
        insertPanel.add(message);
        insertPanel.add(MainMessage);
        insertPanel.add(MainText);
        insertPanel.add(DetailMessage);
        addScorll(DetailText, 10, 100, 430, 160);
        insertPanel.add(SolveMessage);
        addScorll(SolvedText, 10, 300, 430, 160);
        insertPanel.add(isSolved);
        answerIt.setBounds(465, 300, 120, 30);
        deleteIt.setBounds(465, 360, 120, 30);
        root.setBounds(500,405,90,25);
        answerIt.addActionListener(this);
        deleteIt.addActionListener(this);
    }

    private void addScorll(JTextArea jta,int x,int y,int weight,int high){
        JScrollPane newJScroll = new JScrollPane(jta);
        newJScroll.setBounds(x,y,weight,high);
        insertPanel.add(newJScroll);
    }

    private void Delete(String key) throws SQLException {
        PcProblem.delete(key,"Problem");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==deleteIt){
            if(op.isAD) {
                try {
                    Delete((String)SResult[1]);//SResult[1] 表主键
                    JOptionPane.showMessageDialog(this, "删除成功", "提示", JOptionPane.PLAIN_MESSAGE);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                dispose();
            }else JOptionPane.showMessageDialog(this, "权限不够", "提示", JOptionPane.PLAIN_MESSAGE);
        }
        if(e.getSource()==answerIt){
            new AnswerUI(this,"Answering",true,(String)SResult[1],SResult);//SResult[1] 表主键
            dispose();
        }
    }
}
